<?php
namespace Modules\Box\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Box\Entities\Box;
use \Modules\Box\Entities\BoxRank;
use \Modules\Box\Entities\BoxOrder;

use \Modules\Order\Entities\OrderSku;

use \Modules\Asset\Entities\PackageSku;

use Validator;
use ThrowException;
use DB;
use Cache;


// 盲盒
class BoxController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        // $options['with'] = ['small_boxes'];
        $options['with'] = ['image_tag'];

        $options['scopes'] = ['onStock'];

        $options['order_by'] = [
            'list_weight' => 'desc'
        ];

        $options['only'] = [
            'id',
            'uuid',
            'ip',
            'title',
            'tags',
            'thumb',
            'money_price',
            'score_price',
            'discount_money_price',
            'discount_score_price',
            'IP',
            'is_presell',
            'activity_type_text',
            'activity_type',
            'image_tag' => [
                // 'title',
                'image',
                'location'
            ]
        ];

        $list = Box::getList($options);

        return $list->generateListResponse();
    }

    public function show(Request $req, $uuid)
    {
        $item = Box::getEntity([
            'uuid' => $uuid,
            'with' => ['skus', 'lottery'],
        ]);

        $info = $item->getInfo([
            'id',
            'uuid',
            'thumb',
            'title',
            'detail_images',
            'box_top_image',
            'sku_thumb',
            'sku_3d_image',
            'money_price',
            'score_price',
            'discount_money_price',
            'discount_score_price',
            'sku_total',
            'tips',
            'is_presell',
            'is_hidden_sku_rank',
            'big_box_top',
            'big_box_bottom',
            'room_bg_image',
            'small_box_image',
            'sku_ratio_type',
            'bg_color_mode',
            'theme_mode',
            'presell_date',
            'presell_mode',
            'created_at',
            'normal_skus',
            'lottery' => [
            	'id',
            	'uuid',
            	'title'
            ]
        ]);

        if ($req->input('room_id'))
            $room = $item->rooms()->where('id', $req->room_id)->first();
        else 
            $room = $item->randRoom();

        if ($room) {
            $info['room'] = $room->getInfo([
                'id',
                'uuid',
                'number'
            ]);

            $info['room']['sku_status'] = $room->sku_status;
        }
        else
            $info['room'] = NULL;

        return [
            'info' => $info
        ];
    }

    // 切换下一个房间
    public function nextRoom(Request $req, $uuid)
    {
        $currentRoomId = $req->current_room_id;

        $item = Box::getEntity([
            'uuid' => $uuid
        ]);

        $room = $item->randRoom($currentRoomId);

        if ($room) {
            $info = $room->getInfo([
                'id',
                'uuid',
                'number'
            ]);

            $info['sku_status'] = $room->sku_status;
        }
        else
            $info = NULL;

        return [
            'room' => $info
        ];
    }

    // 隐藏款榜单
    public function hiddenSkuRank(Request $req, $uuid)
    {

        $cacheKey = 'rank_' . $uuid;

        $res = Cache::get($cacheKey);

        if (!$res) {

            $list = PackageSku::getList([
                'with' => ['user'],
                'where' => [
                    'node_uuid' => $uuid,
                    'is_special_sku' => 1
                ],
                'per_page' => 50
            ]);

            $res = $list->generateListResponse();

            Cache::put($cacheKey, $res, 8);
        }

        return $res;
    } 
}
